import 'package:flutter/material.dart';
import 'package:flutter_worker730/api/home.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  Map notifyDetail = {
    "id": " ",
    "content": " ",
    "title": " ",
    "createdAt": "  ",
    "creatorName": " "
  };
  // 当传递参数发生变化的时候会执行
  // 生命周期中获取命名路由传参
  // 外部传入的数据改变时自动调用 => 获取路由传参
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // var res =
    //     ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>;
    // print(res['id']);
    var res = ModalRoute.of(context)?.settings.arguments;
    if (res != null) {
      String id = (res as Map<String, dynamic>)['id'];
      getNotifyDetail(id);
    }
  }

  getNotifyDetail(String id) async {
    var res = await getNotifyDetailAPI(id);
    notifyDetail = res;
    //用于触发UI更新的核心方法  作用:通知框架：当前 Widget 的状态已发生变化，需要重新构建 UI
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('公告详情'),
      ),
      body: ListView(
        children: [
          Container(
            padding: const EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                //标题
                Text(
                  notifyDetail['title'] ?? '',
                  style: const TextStyle(
                      fontSize: 18, fontWeight: FontWeight.bold),
                ),
                const SizedBox(
                  height: 10,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      notifyDetail['creatorName'] ?? '',
                      style: const TextStyle(fontSize: 14, color: Colors.grey),
                    ),
                    Text(
                      notifyDetail['createdAt'] ?? '',
                      style: const TextStyle(fontSize: 14, color: Colors.grey),
                    ),
                  ],
                ),
                const SizedBox(
                  height: 10,
                ),
                //内容
                Text(
                  notifyDetail['content'] ?? "",
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
